extends Node

class_name BattleStateMachine

@export var initial_state: BattleState
var states := {}
var current_state: BattleState
var release_card_view
var draw_card_mode = 0

func _ready():
	Events.next_round.connect(_on_next_round_pressed)

func init():
	for child in get_children():
		if child is BattleState:
			states[child.state] = child
			child.transition_requested.connect(_on_transition_requested)
	if initial_state:
		current_state = initial_state
		initial_state.enter()


func _on_transition_requested(from: BattleState, to: BattleState.State):
	if from != current_state:
		return
	var new_state: BattleState = states[to]
	if not new_state:
		return
	if current_state:
		current_state.exit()
	current_state = new_state
	current_state.enter()

func _on_next_round_pressed():
	if current_state:
		current_state.on_next_round_pressed()
